<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes"/>
<title>C1讲解 | pansis.io</title>
<link rel="shortcut icon" href="https://github.pansis.site/favicon.ico">
<link href="https://github.pansis.site/styles/main.css" rel="stylesheet">
<link href="//at.alicdn.com/t/c/font_1678829_b85ccgkdqkr.css" rel="stylesheet">
<link href="//cdnjs.cloudflare.com/ajax/libs/KaTeX/0.10.0/katex.min.css" rel="stylesheet">
<link rel="alternate" type="application/rss+xml" title="pansis.io » Feed" href="https://github.pansis.site/atom.xml">
        <meta name="description" content="
C1
A

1、C语言程序基本结构
#include &lt;stdio.h&gt;   //引用头文件，包含程序所需的一些函数
int main()//main函数，程序入口。int向系统返回值。
{
    printf(&quot;..." />
        <meta name="keywords" content="比赛" />
        <!-- OG -->
        <meta property="og:locale" content="zh_CN">
        <meta property="og:title" content="C1讲解" />
        <meta property="og:type" content="article" />
        <meta property="og:description" content="
C1
A

1、C语言程序基本结构
#include &amp;lt;stdio.h&amp;gt;   //引用头文件，包含程序所需的一些函数
int main()//main函数，程序入口。int向系统返回值。
{
    printf(&amp;quot;...">
        <meta property="og:url" content="https://github.pansis.site/post/c1讲解/" />
        <meta property="og:site_name" content="pansis.io">
        <meta property="og:updated_time" content="2023-09-13">
        <meta property="og:image" content="" />
        <meta property="og:image:secure_url" content="">
        <meta property="og:image:alt" content="C1讲解">
        <!-- Twitter (post.ejs) -->
        <meta name="twitter:card" content="summary_large_image">
        <meta name="twitter:title" content="C1讲解">
        <meta name="twitter:description" content="
C1
A

1、C语言程序基本结构
#include &amp;lt;stdio.h&amp;gt;   //引用头文件，包含程序所需的一些函数
int main()//main函数，程序入口。int向系统返回值。
{
    printf(&amp;quot;...">
        <!-- <meta name="twitter:site" content="@WBoy0609">
        <meta name="twitter:creator" content="@WBoy0609"> -->
        <meta name="twitter:image" content="">
</head>

<body>
    <div class="main animated">
        <div class="header animated fadeInDown">
    <div class="site_title_container">
        <div class="site_title">
            <a href="https://github.pansis.site">pansis.io</a>
        </div>
    </div>
    <div class="my_socials">
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
        <a href="https://github.pansis.site/atom.xml" title="rss" target="_blank"><i class="iconfont icon-rss"></i></a>
    </div>
</div>

    <div class="header_menu">
        
            
                <a href="/" class="menu">首页</a>
            
        
            
                <a href="/tag/GWAaV2nvk/" class="menu">程序设计课程</a>
            
        
            
                <a href="/tag/24hangc" class="menu">比赛</a>
            
        
            
                <a href="/tag/L7r9STb75/" class="menu">Python教程</a>
            
        
            
                <a href="/tags" class="menu">分类</a>
            
        
        <div class="gridea-search-div">
            <form id="gridea-search-form" action="https://github.pansis.site/search/">
                <input class="gridea-search-input" autocomplete="off" spellcheck="false" name="q"/>
            </form>
        </div>
    </div>

            <div class="autopagerize_page_element">
                <div class="content">
                    <div class="post_page">
                        <div class="post animated fadeInDown">
                            <div class="post_title post_detail_title">
                                <h2>
                                    C1讲解
                                </h2>
                                <span class="article-info">
                                    2023-09-13, 714 words, 4 min read
                                </span>
                            </div>
                            <div class="post_content markdown">
                                <p class="md_block">
                                    <span class="md_line md_line_start md_line_end">
                                        <!-- more -->
<h1 id="c1">C1</h1>
<h2 id="a">A</h2>
<figure data-type="image" tabindex="1"><img src="http://cos.pansis.site/202309132053081.png" alt="image-20230913205351982" loading="lazy"></figure>
<h3 id="1-c语言程序基本结构">1、C语言程序基本结构</h3>
<pre><code class="language-c">#include &lt;stdio.h&gt;   //引用头文件，包含程序所需的一些函数
int main()//main函数，程序入口。int向系统返回值。
{
    printf(&quot;FoR rIFt ENergY uNnecEsSary iT iS?\n&quot;
           &quot;oUtPut mAXimiZeD,BUg mAss pROducti0n...\n&quot;
           &quot;hEll0,WOrlD!?&quot;);
}
</code></pre>
<p>2、题目所给的代码块是可以复制的，别真一个字母一个字母手打呀。</p>
<h2 id="b">B</h2>
<h3 id="1-循环结构">1、循环结构</h3>
<pre><code class="language-c">while (&lt;循环条件&gt;)
{ }
for (&lt;初始化&gt;; &lt;循环条件&gt;; &lt;每次循环结束执行的语句&gt;) {
        
    }
</code></pre>
<figure data-type="image" tabindex="2"><img src="http://cos.pansis.site/202309132057754.png" alt="image-20230913205700648" loading="lazy"></figure>
<h3 id="2-表示循环n次">2、表示循环n次</h3>
<pre><code class="language-c">for (int i = 0; i &lt; n; i++) {
        putchar('+');
    }
while (n--)
    printf(&quot;+&quot;);
</code></pre>
<p>代码</p>
<pre><code class="language-c">#include &lt;stdio.h&gt;
int main()
{
    int n;
    scanf(&quot;%d&quot;,&amp;n);
    for (int i = 0; i &lt; n; ++i) {
        printf(&quot;+&quot;);
    }
}
</code></pre>
<h2 id="c">C</h2>
<h4 id="1-scanf纯数字输入">1、scanf纯数字输入</h4>
<pre><code class="language-c">int n,m;
scanf(&quot;%d%d&quot;,&amp;n,&amp;m);
</code></pre>
<p>%d告诉scanf，要从缓冲区读一个数字，scanf会尝试从缓冲区中读入数字。</p>
<ul>
<li>如果遇到空格或回车，则会忽略该空格（同时缓冲区内也不会再有这个空格），直至遇到数字。</li>
<li>如果遇到字母，则该%d读取赋值失败。同时该字母还会留在缓冲区。</li>
<li>scanf在赋值的同时也会有返回值，返回值为成功赋值的变量个数</li>
</ul>
<h3 id="2-常见错误">2、常见错误<img src="http://cos.pansis.site/202309132109884.png" alt="image-20230913210902851" loading="lazy"></h3>
<p>忘记回车、或者缺少空格。</p>
<figure data-type="image" tabindex="3"><img src="http://cos.pansis.site/202309132109464.png" alt="image-20230913210957358" loading="lazy"></figure>
<pre><code class="language-c">#include &lt;stdio.h&gt;
int main()
{
    int n,m;
    scanf(&quot;%d%d&quot;,&amp;n,&amp;m);
    n-=((n-m)*2);
    printf(&quot;%d\n&quot;,n);
    if (n&gt;=4)
        printf(&quot;Yes&quot;);
    else
        printf(&quot;No&quot;);
}

</code></pre>
<h2 id="d">D</h2>
<figure data-type="image" tabindex="4"><img src="http://cos.pansis.site/202309132111744.png" alt="image-20230913211139626" loading="lazy"></figure>
<figure data-type="image" tabindex="5"><img src="http://cos.pansis.site/202309132111575.png" alt="image-20230913211147543" loading="lazy"></figure>
<pre><code class="language-c">#include &lt;stdio.h&gt;
int main()
{
    int x1,y1,z1,x2,y2,z2;
    scanf(&quot;%d%d%d&quot;,&amp;x1,&amp;y1,&amp;z1);
    scanf(&quot;%d%d%d&quot;,&amp;x2,&amp;y2,&amp;z2);
    printf(&quot;%d&quot;,x1*x2+y1*y2+z1*z2);
}
</code></pre>
<h2 id="e">E</h2>
<figure data-type="image" tabindex="6"><img src="http://cos.pansis.site/202309132113582.png" alt="image-20230913211333492" loading="lazy"></figure>
<pre><code class="language-c">#include &lt;stdio.h&gt;
int main() {
    int n, a, b, i;
    scanf(&quot;%d&quot;, &amp;n);
    for (i = 0; i &lt; n; i++) {
        scanf(&quot;%d%d&quot;, &amp;a, &amp;b);
        if (a == 0) {
            printf(&quot;Wait. That's illegal.\n&quot;);
        } else {
            printf(&quot;%d mod %d = %d\n&quot;, b, a, b % a);
            // 注意题干 计算的是 b mod a 而不是 a mod b
        }
    }
    return 0;
}
</code></pre>
<h3 id="不定多组输入问题">不定多组输入问题</h3>
<pre><code class="language-c">while ((scanf(&quot;%d%d&quot;,&amp;a,&amp;b))!=EOF)
{
    
}

</code></pre>
<p>EOF本质就是数字-1，当scanf遇到文件尾（键盘的输入字符ctrl+z（dev），ctrl+d（clion））时就会返回-1</p>
<pre><code class="language-c"> while (gets(a) != NULL)
{
    
}
</code></pre>
<h2 id="f">F</h2>
<figure data-type="image" tabindex="7"><img src="http://cos.pansis.site/202309132120765.png" alt="image-20230913212057622" loading="lazy"></figure>
<pre><code class="language-c">#include &lt;stdio.h&gt;
int main()
{
    int n;
    int a;
    int sum=0;int l=0;
    scanf(&quot;%d&quot;,&amp;n);
    for (int i = 0; i &lt; n; ++i) {
        scanf(&quot;%d&quot;,&amp;a);
        if (a!=0)
        {
            sum+=a;
            l++;
        }
    }
    if (l==0)
    {
        printf(&quot;Oh!No!&quot;);
        return 0;
    }
    printf(&quot;%d&quot;,sum/l);
}
</code></pre>
<h3 id="整整问题">整/整问题</h3>
<figure data-type="image" tabindex="8"><img src="http://cos.pansis.site/202309132122971.png" alt="image-20230913212214927" loading="lazy"></figure>
<p>C语言中整数除以整数=整数。3/2=1  3/2.0=1.5</p>
<h2 id="g">G</h2>
<figure data-type="image" tabindex="9"><img src="http://cos.pansis.site/202309132123766.png" alt="image-20230913212349632" loading="lazy"></figure>
<pre><code class="language-c">#include &lt;stdio.h&gt;
int main()
{
	int x, cnt;
	scanf(&quot;%d&quot;, &amp;x);
	for(cnt = 0; x != 1; ++cnt)
		if(x % 2 == 1) x = 3 * x + 1;
		else x = x / 2;
	printf(&quot;%d&quot;, cnt);
	return 0;
}
</code></pre>
<h2 id="常见错误">常见错误</h2>
<p>1、scanf &amp;问题</p>
<p>2、分号问题</p>
<p>3、变量定义与初始化问题</p>
<br />
                                            
                                </p>
                            </div>
                            <div class="post_footer">
                                
                                    <div class="meta">
                                        <div class="info"><span class="field tags"><i class="iconfont icon-tag-sm"></i>
                                                
                                                    <a href="https://github.pansis.site/tag/0kvGbp_AE/" class="article-info">
                                                        比赛
                                                    </a>
                                                    
                                            </span>
                                        </div>
                                    </div>
                                    
                                        
                            </div>
                        </div>
                        
                            
                                <link rel="stylesheet" href="https://unpkg.com/gitalk/dist/gitalk.css">
<script src="https://unpkg.com/gitalk/dist/gitalk.min.js"></script>
<div id="gitalk-container" style="padding-bottom: 20px;"></div>
<script>
    var pageId = (location.pathname).substring(1, 49) // Ensure uniqueness and length less than 50
    pageId = pageId.endsWith('/') ? pageId.slice(0, -1) : pageId // 以斜杠结尾则去除
    var gitalk = new Gitalk({
        clientID: '9d5eba33618472c44a07',
        clientSecret: '065a85ed04333ceebfc4f01d7ca1674175730339',
        repo: 'fzxl2003.github.io',
        owner: 'fzxl2003',
        admin: ['fzxl2003'],
        id: pageId,
        distractionFreeMode: false  // Facebook-like distraction free mode
    })
    gitalk.render('gitalk-container')
</script>
                                    
                                        
                                                    
                    </div>
                </div>
            </div>
    </div>
    <div class="footer">
    
    <div class="powered_by">
        <a href="https://codeberg.org/kytrun/gridea-theme-one" target="_blank">Theme One,</a>
        <a href="https://open.gridea.dev/" target="_blank">Powered by Gridea&#65281;</a>
    </div>
    
    
        <div class="footer_slogan">
            Powered by <a href="https://github.com/getgridea/gridea" target="_blank">Gridea</a>
        </div>
    
    <div id="back_to_top" class="back_to_top">
        <span>△</span>
    </div>
    
</div>

<script src="https://github.pansis.site/media/scripts/util.js"></script>
        <link rel="stylesheet" href="//unpkg.com/@highlightjs/cdn-assets@11.5.1/styles/default.min.css">
        <script src="//unpkg.com/@highlightjs/cdn-assets@11.5.1/highlight.min.js"></script>
        <script>hljs.highlightAll();</script>
</body>

</html>